package com.sgb.kjwl.api;

import com.sgb.kjwl.api.service.BidAPI;
import com.sgb.kjwl.api.service.BigHomeAPI;
import com.sgb.kjwl.api.service.BusinessManagementAPI;
import com.sgb.kjwl.api.service.BusinessOpportunitiesAPI;
import com.sgb.jicai.service.CentralizedMiningAPI;
import com.sgb.kjwl.api.service.CommonAPI;
import com.sgb.kjwl.api.service.ContactAPI;
import com.sgb.kjwl.api.service.DeviceAPI;
import com.sgb.kjwl.api.service.EnterpriseAPI;
import com.sgb.kjwl.api.service.FactoryAPI;
import com.sgb.kjwl.api.service.GoodsAPI;
import com.sgb.kjwl.api.service.HomeLeaseAPI;
import com.sgb.kjwl.api.service.IMServiceAPI;
import com.sgb.kjwl.api.service.InquiryAPI;
import com.sgb.kjwl.api.service.LandAPI;
import com.sgb.kjwl.api.service.LeaseAPI;
import com.sgb.kjwl.api.service.LeaseMapAPI;
import com.sgb.kjwl.api.service.LookingforAJobAPI;
import com.sgb.kjwl.api.service.MachineAPI;
import com.sgb.kjwl.api.service.MailListAPI;
import com.sgb.kjwl.api.service.ManageAPI;
import com.sgb.kjwl.api.service.MaterialAPI;
import com.sgb.kjwl.api.service.NeedAPI;
import com.sgb.kjwl.api.service.OrderAPI;
import com.sgb.kjwl.api.service.PackerAPI;
import com.sgb.kjwl.api.service.PickAPI;
import com.sgb.kjwl.api.service.PowerAPI;
import com.sgb.kjwl.api.service.RecruitmentHomeAPI;
import com.sgb.kjwl.api.service.SelfGoodsPublishAPI;
import com.sgb.kjwl.api.service.SelfOwnerAPI;
import com.sgb.kjwl.api.service.ShareApi;
import com.sgb.kjwl.api.service.ShopCarOrderAPI;
import com.sgb.kjwl.api.service.ShoppingCarAPI;
import com.sgb.kjwl.api.service.ShoppingCarNewOrderAPI;
import com.sgb.kjwl.api.service.TeamPriceAPI;
import com.sgb.kjwl.api.service.TransAPI;
import com.sgb.kjwl.api.service.UploadAPI;
import com.sgb.kjwl.api.service.UserAPI;
import com.sgb.kjwl.api.service.WebsiteInfoAPI;
import com.sgb.kjwl.model.api.DriverAPI;
import com.sgb.kjwl.view.ui.activity.toBeReviewed.ToReviewAPI;
import com.swgk.core.base.model.factory.BaseUrlInterceptor;
import com.swgk.core.base.model.factory.CommonInterceptor;
import com.swgk.core.base.model.factory.CryptoConverterFactory;
import com.swgk.core.util.MLog;

import java.util.concurrent.TimeUnit;

import okhttp3.Call;
import okhttp3.OkHttpClient;
import retrofit2.Retrofit;

@Deprecated
public class APIManager {
    private static final String TAG = "api";
    public static final String RESPONSE_OK = "ok";
    public static final String REQUEST_SUCCESS = "0";
    public static final String RESPONSE_FAIL = "fail";
    private static APIManager apiManager;

    /**
     * 车辆状态信息
     */
    //异常
    public static final String CAR_ABNORMAL = "222";
    /**
     * apiClass
     */
    private GoodsAPI goodsAPI;
    private ShoppingCarAPI shoppingCarAPI;
    private UserAPI userAPI;
    private OrderAPI orderAPI;
    private UploadAPI uploadAPI;
    private MaterialAPI materialAPI;
    private MachineAPI machineAPI;
    private DeviceAPI deviceAPI;
    private LeaseMapAPI leaseMapAPI;
    private PowerAPI powerAPI;
    private PackerAPI packerAPI;
    private PickAPI pickAPI;
    private CommonAPI commonAPI;
    private NeedAPI mNeedAPI;
    private ShareApi shareapi;
    private RecruitmentHomeAPI recruitmentHomeAPI;
    private BusinessOpportunitiesAPI businessOpportunitiesAPI;
    private BusinessManagementAPI businessManagementAPI;
    private BidAPI bidAPI;
    private SelfGoodsPublishAPI selfGoodsPublishAPI;
    private MailListAPI mailListAPI;
    private HomeLeaseAPI homeLeaseAPI;
    private CentralizedMiningAPI centralizedMiningAPI;
    private DriverAPI driverAPI;
    private LookingforAJobAPI lookingforAJobAPI;
    private TeamPriceAPI teamPriceAPI;
    private BigHomeAPI bigHomeAPI;
    private ContactAPI contactAPI;
    private ToReviewAPI toReviewAPI;
    private SelfOwnerAPI selfOwnerAPI;
    private TransAPI transAPI;
    private LeaseAPI leaseAPI;
    private EnterpriseAPI enterpriseAPI;
    private FactoryAPI factoryAPI;
    private LandAPI landAPI;
    private InquiryAPI inquiryAPI;
    private ManageAPI manageAPI;
    private WebsiteInfoAPI websiteInfoAPI;
    private ShopCarOrderAPI shopCarOrderAPI;
    private ShoppingCarNewOrderAPI shoppingCarNewOrderAPI;
    private IMServiceAPI imServiceAPI;

    /**
     * 短响应时间请求 10秒超时
     */
    private OkHttpClient okHttpClient;
    private Retrofit localTestRetrofit;
    /**
     * 长响应时间请求 150秒超时
     */
    private OkHttpClient uploadHttpClient;
    private Retrofit localTestRetrofitLong;

    /**
     * 接口初始化入口
     *
     * @return
     */
    public static APIManager getInstance() {
        if (apiManager == null) {
            apiManager = new APIManager();
        }
        return apiManager;
    }

    /**
     * 初始化
     * 使用默认的baseUrl
     */
    private APIManager() {
        if (okHttpClient == null || uploadHttpClient == null) {
            initOkHttpClient();
        }
        initRetrofit(UrlConfig.BASE_URL);
        initServiceClass();
    }

    /**
     * 当切换BaseUrl时调用
     *
     * @param baseUrl
     */
    public void changeAPIManager(String baseUrl) {
        if (okHttpClient == null || uploadHttpClient == null) {
            initOkHttpClient();
        }
        initRetrofit(baseUrl);
        initServiceClass();
    }

    /**
     * 初始化  OkHttpClient
     */
    private void initOkHttpClient() {
        OkHttpClient.Builder okHttpBuilder =
                new OkHttpClient.Builder()
                        .addInterceptor(new BaseUrlInterceptor())
                        .addInterceptor(new CommonInterceptor(null));

        okHttpClient = okHttpBuilder.connectTimeout(10, TimeUnit.SECONDS).readTimeout(10, TimeUnit.SECONDS)
                .writeTimeout(10, TimeUnit.SECONDS).build();

        uploadHttpClient = okHttpBuilder.connectTimeout(150, TimeUnit.SECONDS).readTimeout(150, TimeUnit.SECONDS)
                .writeTimeout(150, TimeUnit.SECONDS).build();
    }

    /**
     * 初始化 Retrofit
     *
     * @param baseUrl 当前接口访问的域名
     */
    private void initRetrofit(String baseUrl) {
        localTestRetrofit = new Retrofit.Builder()
                .baseUrl(baseUrl)
                .client(okHttpClient)
                .addConverterFactory(CryptoConverterFactory.create())
                .build();

        localTestRetrofitLong = new Retrofit.Builder()
                .baseUrl(baseUrl)
                .callFactory(uploadHttpClient)
                .addConverterFactory(CryptoConverterFactory.create())
                .build();
    }

    public <T> T createApi(Class<T> tClass){
        return localTestRetrofit.create(tClass);
    }

    /**
     * 取消网络请求请求，请求中接口取消会走失败的方法，队列中等待接口直接会取消
     */
    public void cancelOkHttpCalls(boolean isCancelAll) {
        MLog.d("apiLog", "cancelOkHttpCalls");
        if (okHttpClient == null || okHttpClient.dispatcher() == null) return;
        MLog.d("apiLog", String.format("queuedCount=%s,runningCount=%s",
                okHttpClient.dispatcher().queuedCallsCount(),
                okHttpClient.dispatcher().runningCallsCount()));
        //判断是否取消全部请求
        if (isCancelAll) {
            okHttpClient.dispatcher().cancelAll();
            return;
        }
        //取消所有队列中的请求
        for (Call call : okHttpClient.dispatcher().queuedCalls()) {
            call.cancel();
        }
    }

    private void initServiceClass() {
        uploadAPI = localTestRetrofitLong.create(UploadAPI.class);

        goodsAPI = localTestRetrofit.create(GoodsAPI.class);

        shoppingCarAPI = localTestRetrofit.create(ShoppingCarAPI.class);

        userAPI = localTestRetrofit.create(UserAPI.class);

        materialAPI = localTestRetrofit.create(MaterialAPI.class);

        machineAPI = localTestRetrofit.create(MachineAPI.class);

        deviceAPI = localTestRetrofit.create(DeviceAPI.class);

        leaseMapAPI = localTestRetrofit.create(LeaseMapAPI.class);

        packerAPI = localTestRetrofit.create(PackerAPI.class);

        pickAPI = localTestRetrofit.create(PickAPI.class);

        powerAPI = localTestRetrofit.create(PowerAPI.class);

        orderAPI = localTestRetrofit.create(OrderAPI.class);

        commonAPI = localTestRetrofit.create(CommonAPI.class);

        mNeedAPI = localTestRetrofit.create(NeedAPI.class);

        shareapi = localTestRetrofit.create(ShareApi.class);

        recruitmentHomeAPI = localTestRetrofit.create(RecruitmentHomeAPI.class);

        businessOpportunitiesAPI = localTestRetrofit.create(BusinessOpportunitiesAPI.class);

        businessManagementAPI = localTestRetrofit.create(BusinessManagementAPI.class);


        bidAPI = localTestRetrofit.create(BidAPI.class);

        selfGoodsPublishAPI = localTestRetrofit.create(SelfGoodsPublishAPI.class);

        mailListAPI = localTestRetrofit.create(MailListAPI.class);

        homeLeaseAPI = localTestRetrofit.create(HomeLeaseAPI.class);

        centralizedMiningAPI = localTestRetrofit.create(CentralizedMiningAPI.class);

        driverAPI = localTestRetrofit.create(DriverAPI.class);

        lookingforAJobAPI = localTestRetrofit.create(LookingforAJobAPI.class);

        teamPriceAPI = localTestRetrofit.create(TeamPriceAPI.class);

        bigHomeAPI = localTestRetrofit.create(BigHomeAPI.class);

        contactAPI = localTestRetrofit.create(ContactAPI.class);

        toReviewAPI = localTestRetrofit.create(ToReviewAPI.class);

        selfOwnerAPI = localTestRetrofit.create(SelfOwnerAPI.class);

        transAPI = localTestRetrofit.create(TransAPI.class);

        leaseAPI = localTestRetrofit.create(LeaseAPI.class);

        enterpriseAPI = localTestRetrofit.create(EnterpriseAPI.class);

        factoryAPI = localTestRetrofit.create(FactoryAPI.class);

        landAPI = localTestRetrofit.create(LandAPI.class);

        inquiryAPI = localTestRetrofit.create(InquiryAPI.class);

        manageAPI = localTestRetrofit.create(ManageAPI.class);

        websiteInfoAPI = localTestRetrofit.create(WebsiteInfoAPI.class);

        shopCarOrderAPI = localTestRetrofit.create(ShopCarOrderAPI.class);
        shoppingCarNewOrderAPI = localTestRetrofit.create(ShoppingCarNewOrderAPI.class);

        imServiceAPI = localTestRetrofit.create(IMServiceAPI.class);

    }

    public GoodsAPI getGoodsAPI() {
        return this.goodsAPI;
    }

    public TransAPI getTransAPI() {
        return this.transAPI;
    }

    public ShoppingCarAPI getShoppingCarAPI() {
        return this.shoppingCarAPI;
    }

    public UserAPI getUserAPI() {
        return userAPI;
    }

    public UploadAPI getUploadAPI() {
        return uploadAPI;
    }

    public MaterialAPI getMaterialAPI() {
        return materialAPI;
    }

    public MachineAPI getMachineAPI() {
        return machineAPI;
    }

    public DeviceAPI getDeviceAPI() {
        return deviceAPI;
    }

    public LeaseMapAPI getLeaseMapAPI() {
        return leaseMapAPI;
    }

    public PackerAPI getPackerAPI() {
        return packerAPI;
    }

    public PowerAPI getPowerAPI() {
        return powerAPI;
    }

    public OrderAPI getOrderAPI() {
        return orderAPI;
    }

    public PickAPI getPickAPI() {
        return pickAPI;
    }

    public CommonAPI getCommonAPI() {
        return commonAPI;
    }

    public NeedAPI getNeedAPI() {
        return mNeedAPI;
    }

    public ShareApi getShareApi() {
        return shareapi;
    }

    public RecruitmentHomeAPI getRecruitmentHomeAPI() {
        return recruitmentHomeAPI;
    }

    public BusinessOpportunitiesAPI getBusinessOpportunitiesAPI() {
        return businessOpportunitiesAPI;
    }

    public BusinessManagementAPI getBusinessManagementAPI() {
        return businessManagementAPI;
    }



    public BidAPI getBidAPI() {
        return bidAPI;
    }

    public SelfGoodsPublishAPI getSelfGoodsPublishAPI() {
        return selfGoodsPublishAPI;
    }

    public MailListAPI getMailListAPI() {
        return mailListAPI;
    }

    public HomeLeaseAPI getHomeLeaseAPI() {
        return homeLeaseAPI;
    }

    public CentralizedMiningAPI getCentralizedMiningAPI() {
        return centralizedMiningAPI;
    }

    public DriverAPI getDriverAPI() {
        return this.driverAPI;
    }

    public LookingforAJobAPI getLookingforAJobAPI() {
        return lookingforAJobAPI;
    }

    public TeamPriceAPI getTeamPriceAPI() {
        return teamPriceAPI;
    }

    public BigHomeAPI getBigHomeAPI() {
        return bigHomeAPI;
    }

    public ContactAPI getContactAPI() {
        return contactAPI;
    }

    public ToReviewAPI getToReviewAPI() {
        return toReviewAPI;
    }

    public SelfOwnerAPI getSelfOwnerAPI() {
        return selfOwnerAPI;
    }

    public LeaseAPI getLeaseAPI() {
        return this.leaseAPI;
    }

    public EnterpriseAPI getEnterpriseAPI() {
        return this.enterpriseAPI;
    }

    public FactoryAPI getFactoryAPI() {
        return factoryAPI;
    }

    public LandAPI getLandAPI() {
        return landAPI;
    }

    public InquiryAPI getInquiryAPI() {
        return inquiryAPI;
    }

    public ManageAPI getManageAPI() {
        return manageAPI;
    }


    public WebsiteInfoAPI getWebsiteInfoAPI() {
        return websiteInfoAPI;
    }

    public ShopCarOrderAPI getShopCarOrderAPI() {
        return shopCarOrderAPI;
    }
    public ShoppingCarNewOrderAPI getShoppingCarNewOrderAPI() {
        return shoppingCarNewOrderAPI;
    }

    public IMServiceAPI getIMServiceAPI(){
        return imServiceAPI;
    }
}
